package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.domain.Admin;
import com.itheima.domain.PageBean;
import com.itheima.mapper.AdminMapper;
import com.itheima.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    private AdminMapper adminMapper;
    @Override
    public PageBean<Admin> findAll(String _currentPage,String _rows) {
        PageBean<Admin> pb = new PageBean<>();
        int currentPage = Integer.parseInt(_currentPage);
        pb.setCurrentPage(currentPage);
        int rows = Integer.parseInt(_rows);
        pb.setRows(rows);
        PageHelper.startPage(currentPage,rows);
        List<Admin> admins = adminMapper.findAll();
        pb.setList(admins);
        PageInfo<Admin> pageInfo = new PageInfo<Admin>(admins);
        int totalPage = (int)pageInfo.getPages();
        pb.setTotalPage(totalPage);
        int totalCount = (int)pageInfo.getTotal();
        pb.setTotalCount(totalCount);
        return pb;
    }

    @Override
    public Admin findByUsernameAndPassword(String username, String password) {
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("username",username);
        map.put("password",password);
        Admin admin = adminMapper.findByUsernameAndPassword(map);
        System.out.println(admin);
        return admin;
    }

    @Override
    public void deleteByAid(Integer id) {
        adminMapper.deleteByAid(id);
    }

    @Override
    public Admin findByUsername(String username) {
        Admin admin = adminMapper.findByUsername(username);
        return admin;
    }

    @Override
    public void saveAdmin(Admin admin) {
        adminMapper.saveAdmin(admin);
    }
}
